﻿using System.Linq.Expressions;

namespace HelperFromTheSkeleton.Convert
{
    public static class LinqHelper
    {
        /// <summary>
        /// 并且
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <param name="andExpression"></param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> And<T>(this  Expression<Func<T, bool>> expression, Expression<Func<T, bool>> andExpression)
        {
            var invokedExpr = Expression.Invoke(andExpression, expression.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>(Expression.And(expression.Body, invokedExpr), expression.Parameters);
        }

        /// <summary>
        /// 或者
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <param name="andExpression"></param>
        /// <returns></returns>
        public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expression, Expression<Func<T, bool>> andExpression)
        {
            var invokedExpr = Expression.Invoke(andExpression, expression.Parameters.Cast<Expression>());
            return Expression.Lambda<Func<T, bool>>(Expression.Or(expression.Body, invokedExpr), expression.Parameters);
        }
    }
}
